package com.mindskip.xzs.controller.admin;

import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.utility.RestResponse;
import com.mindskip.xzs.domain.dto.admin.education.SubjectCreateDTO;
import com.mindskip.xzs.domain.dto.admin.education.SubjectPageDTO;
import com.mindskip.xzs.domain.entity.Subject;
import com.mindskip.xzs.domain.vo.admin.education.SubjectVO;
import com.mindskip.xzs.service.SubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "学科管理接口")
@RestController("AdminEducationController")
@RequestMapping(value = "/api/admin/education")
public class EducationController {

    private final SubjectService subjectService;

    public EducationController(SubjectService subjectService) {
        this.subjectService = subjectService;
    }

    @ApiOperation(value = "学科列表",httpMethod = "POST")
    @PostMapping("/subject/list")
    public RestResponse<List<Subject>> subjectList() {

        return RestResponse.ok(subjectService.selectAll());
    }

    @ApiOperation(value = "学科列表",httpMethod = "POST")
    @PostMapping("/subject/page")
    public RestResponse<PageInfo<SubjectVO>> subjectPage(@RequestBody SubjectPageDTO subjectPageDTO) {

        PageInfo<SubjectVO> pageInfo = subjectService.getSubjectByPage(subjectPageDTO);
        return RestResponse.ok(pageInfo);
    }

    @ApiOperation(value = "学科查询",httpMethod = "POST")
    @PostMapping("/subject/select/{id}")
    public RestResponse<SubjectVO> subjectSelect(@PathVariable Integer id) {

        Subject subject = subjectService.selectById(id);

        SubjectVO subjectVO = new SubjectVO();
        BeanUtils.copyProperties(subject, subjectVO);
        return RestResponse.ok(subjectVO);
    }

    @ApiOperation(value = "学科编辑",httpMethod = "POST")
    @PostMapping("/subject/edit")
    public RestResponse<Object> subjectEdit(@RequestBody SubjectCreateDTO subjectCreateDTO) {
        subjectService.insertOrUpdateSubject(subjectCreateDTO);
        return null;
    }

    @ApiOperation(value = "学科编辑",httpMethod = "POST")
    @PostMapping("/subject/delete/{id}")
    public RestResponse<Object> subjectDelete(@PathVariable Integer id) {

        // 硬删除
        subjectService.deleteById(id);

        return RestResponse.ok();
    }
}
